local Wnd_LoginClass = class(WindowBase)

function Wnd_LoginClass:Start(WindowIndex)
    self.WindowIndex = WindowIndex
    Event.AddListener(EventType.UILoginEnter, self.Show, self)
    Event.AddListener(EventType.UILoginExit, self.Hide, self)
end

function Wnd_LoginClass:OnNewInstance()
    self.NameInput = Utils.FindChild(self.Instance, "Name"):GetComponent(typeof(InputField))
    self.PasswordInput = Utils.FindChild(self.Instance, "Password"):GetComponent(typeof(InputField))

    self.NameLabel = Utils.FindChild(self.Instance, "Name/Text_AccountName")
    self.PasswordLabel = Utils.FindChild(self.Instance, "Password/Text_AccountName")

    self.LoginBtn = Utils.FindChild(self.Instance, "Login")
    self:Listen(self.LoginBtn, EventTriggerType.PointerClick, self.OnLoginClick)
end

function Wnd_LoginClass:OnShowDone()
    local mail = PlayerPrefs.GetString("PlayerMail")
    local password = PlayerPrefs.GetString("PlayePassword")
    if mail ~= nil then
        self.NameInput.text = mail
    end

    if password ~= nil then
        self.PasswordInput.text = password
    end
end

function Wnd_LoginClass:OnLoginClick(Object)
    local mail = self.NameInput.text
    local password = self.PasswordInput.text
    NetworkSender:AskLogin(mail, password, 0)

    if PlayerPrefs.GetString("PlayerMail") == nil or PlayerPrefs.GetString("PlayerMail") == "" then
        PlayerPrefs.SetString("PlayerMail", mail)
    end

    if PlayerPrefs.GetString("PlayePassword") == nil or PlayerPrefs.GetString("PlayePassword") == "" then
        PlayerPrefs.SetString("PlayePassword", password)
    end
end

function Wnd_LoginClass:OnLostInstance()
    Event.RemoveListener(EventType.UILoginEnter, self.Show)
    Event.RemoveListener(EventType.UILoginExit, self.Hide)
end

return Wnd_LoginClass.new